package cn.shyouchi.wuguanjia.android.ui.patrol.statistic.page;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.ui.patrol.statistic.PatrolStatisticEntity;
import cn.shyouchi.wuguanjia.android.ui.patrol.statistic.page.error.PatrolStatisticErrorFragment;
import cn.shyouchi.wuguanjia.android.ui.patrol.statistic.page.forget.PatrolStatisticForgetFragment;
import cn.shyouchi.wuguanjia.android.ui.patrol.statistic.page.normal.PatrolStatisticNormalFragment;
import cn.shyouchi.wuguanjia.android.widget.TopBar;

public class PreviewPatrolInfoActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.rb_normal)
    RadioButton mRbNormal;
    @BindView(R.id.rb_error)
    RadioButton mRbError;
    @BindView(R.id.rb_forget)
    RadioButton mRbForget;
    @BindView(R.id.rg_group)
    RadioGroup mRgGroup;
    @BindView(R.id.page_error)
    ViewPager mPageError;

    List<Fragment> mFragments = new ArrayList<>();


    List<PatrolStatisticEntity.ResultMessageBean.InfoBean.InspectionBean> normalPoint = new ArrayList<>();
    List<PatrolStatisticEntity.ResultMessageBean.InfoBean.InspectionBean> errorPoint = new ArrayList<>();
    List<PatrolStatisticEntity.ResultMessageBean.InfoBean.InspectionBean> forgetPoint = new ArrayList<>();
    PatrolStatisticEntity.ResultMessageBean.InfoBean model;

    @Override
    public int initContentView() {
        return R.layout.activity_preview_patrol_info;
    }

    @Override
    public void initInjector() {
        model = getIntent().getParcelableExtra("info");
        if (model != null) {
            for (PatrolStatisticEntity.ResultMessageBean.InfoBean.InspectionBean bean : model.getInspection()) {
                if (bean.getSituation() == 0) {
                    errorPoint.add(bean);
                } else if (bean.getSituation() == 1) {
                    normalPoint.add(bean);
                } else if (bean.getSituation() == 2) {
                    forgetPoint.add(bean);
                }
            }
        }
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());

        mFragments.add(new PatrolStatisticNormalFragment());
        mFragments.add(new PatrolStatisticErrorFragment());
        mFragments.add(new PatrolStatisticForgetFragment());

        mPageError.setAdapter(new ErrorPageAdapter());
        mRgGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_normal:
                        mPageError.setCurrentItem(0);
                        break;
                    case R.id.rb_error:
                        mPageError.setCurrentItem(1);
                        break;
                    case R.id.rb_forget:
                        mPageError.setCurrentItem(2);
                        break;

                }
            }
        });

        mPageError.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mRgGroup.check(mRgGroup.getChildAt(position).getId());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }

    private class ErrorPageAdapter extends FragmentPagerAdapter {
        public ErrorPageAdapter() {
            super(getSupportFragmentManager());
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }


    public List<PatrolStatisticEntity.ResultMessageBean.InfoBean.InspectionBean> getNormalPoint() {
        return normalPoint;
    }

    public List<PatrolStatisticEntity.ResultMessageBean.InfoBean.InspectionBean> getErrorPoint() {
        return errorPoint;
    }

    public List<PatrolStatisticEntity.ResultMessageBean.InfoBean.InspectionBean> getForgetPoint() {
        return forgetPoint;
    }

    public PatrolStatisticEntity.ResultMessageBean.InfoBean getModel() {
        return model;
    }
}
